$(function () {

    // 获取标题数据.
    $.ajax({
        type: "get",
        url: "http://193.112.55.79:9090/api/getcategorytitle",
        // data: "json",
        dataType: "json",
        success: function (res) {
            // console.log(res);
            var html = template('categoryTemp', res.result);
            // console.log(html);
            $('.top').html(html);
        }
    });


    FastClick.attach(document.body);
    $('.top').on('click', 'span', function () {
        var that = $(this);
        // 如果span存在兄弟元素ul，就让其上拉并且进行删除操作.
        if (that.siblings('ul').length == 1) {
            // console.log( that.siblings('ul'))
            that.siblings('ul').slideUp();
            setTimeout(function(){
                that.siblings('ul').remove()
            },500)
            return;
        }
        $.ajax({
            type: "get",
            url: "http://193.112.55.79:9090/api/getcategory",
            data: {
                titleid: parseInt($(this).attr('titleId'))
            },
            dataType: "json",
            success: function (res) {
                console.log(res);
                var html = template('commercialId', res.result);
                // console.log(event.target);
                that.parent().append(html);

                that.siblings('ul').slideDown();

            }
        });
    })
});